#pragma once
#include "stdafx.h"
#include "ShowDlg.h"
#include "SendDlg.h"
#include "AudioDlg.h"
#include "VideoDlg.h"
#include "FileDlg.h"

#define WM_CLOSE_CHAT		WM_USER+999
#define WM_SEND_MSG			WM_USER+1000
#define WM_INVITE_VIDEO		WM_USER+1001
#define WM_INVITE_AUIDO		WM_USER+1002
#define WM_INVITE_FILE		WM_USER+1003
#define WM_MSG_AUDIO_ACCEPT	WM_USER+1004
#define WM_MSG_AUDIO_HANGUP WM_USER+1005
#define WM_MSG_VIDEO_ACCEPT	WM_USER+1006
#define WM_MSG_VIDEO_HANGUP	WM_USER+1007
#define WM_MSG_FILE_ACCEPT	WM_USER+1008
#define WM_MSG_FILE_HANGUP	WM_USER+1009

extern  xmpp_mgr	*_pxmpp_client;
class CChatDlg : public CAxDialogImpl<CChatDlg>,public CUpdateUI<CChatDlg>,public CMessageFilter,public CIdleHandler
{
public:
	enum { IDD = IDD_DIALOG_CHAT };
	CSplitterWindow  m_wndVertSplit;
	CHorSplitterWindow   m_wndHoriSplit;
	CAxWindow		m_UserShowWnd;
	CShowDlg		m_showWnd;
	CSendDlg		m_sendWnd;
	VideoDlg		m_videoWnd;
	AudioDlg		m_audioWnd;
	FileDlg			m_fileWnd;
	JID				m_jid;
	string			m_user_sip_addr;
	int				m_offset;
	bool			m_bvideoing;
	bool			m_baudioing;
	bool			m_brequest_v;

	int				m_tid;
	int				m_cid;
	int				m_did;
	string			m_rtp_ip;
	int				m_aport;
	int				m_vport;


	bool			m_bVideoCreate;
	bool			m_bAudioCreate;

	


	string			m_selfusername;

	string			m_newfile;
	string			m_newsid;
	long			m_newsize;


	virtual BOOL PreTranslateMessage(MSG *pMSG)
	{
		if(m_hWnd)
			return CWindow::IsDialogMessage(pMSG);
		return false;
	}
	virtual BOOL OnIdle()
	{
		return false;
	}
	BEGIN_UPDATE_UI_MAP(CChatDlg)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CChatDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_DROPFILES, OnDropFiles)
		MESSAGE_HANDLER(WM_CLOSE, OnClose)
		MESSAGE_HANDLER(WM_SIZE,OnSize)
		MESSAGE_HANDLER(WM_DESTROY,OnDestroy)
		MESSAGE_HANDLER(WM_SEND_MSG, OnSendMsg)
		MESSAGE_HANDLER(WM_INVITE_VIDEO,OnVideo)
		MESSAGE_HANDLER(WM_INVITE_AUIDO,OnAudio)
		MESSAGE_HANDLER(WM_INVITE_FILE,OnFile)
		MESSAGE_HANDLER(WM_MSG_AUDIO_ACCEPT,OnAudioAccept)
		MESSAGE_HANDLER(WM_MSG_AUDIO_HANGUP,OnAudioHangUp)
		MESSAGE_HANDLER(WM_MSG_VIDEO_ACCEPT,OnVideoAccept)
		MESSAGE_HANDLER(WM_MSG_VIDEO_HANGUP,OnVideoHangUp)
		MESSAGE_HANDLER(WM_MSG_FILE_ACCEPT,OnFileAccept)
		MESSAGE_HANDLER(WM_MSG_FILE_HANGUP,OnFileHangUp)
		CHAIN_MSG_MAP(CUpdateUI<CChatDlg>)
	END_MSG_MAP()
	LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnDropFiles(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnSendMsg(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnVideo(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnAudio(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnFile(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnAudioAccept(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnAudioHangUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnVideoAccept(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnVideoHangUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnFileAccept(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT OnFileHangUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	void ShowNewMsg(const Message& msg);
	void ShowSysMsg(string msg,int ntype);
	void ShowCallAudio(bool binitiative);
	void HideCallAudio();
	void ShowCallVideo(bool binitiative);
	void HideCallVideo();
	void ShowFileT(bool binitiaitve);
	void HideFileT();
	
	void SetDestSipAddr(string sip_addr);
	void OnAccept();
	void OnRing();
	void OnACK();
	void OnBye();
	void OnRefuse();
	void OnCanel();
	


	void OnAFTRequest();
	void OnAFTRequest(string file,string sid,long size);
	void OnAFTRequestError(string sid,string text,int ntype=1);
	void OnOffLine();
private:
	bool CreateAudioSend(string ip,unsigned short aport,unsigned short vport);
	bool CreateVideoSend(string ip,unsigned short aport,unsigned short vport);
	bool DeleteAudioSend();
	bool DeleteVideoSend();
	void RecycleAllRes();


};
